package cn.wsn2018.tools.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;

/**
 * 封装Json格式返回状态的工具类，分为成功与失败两种情况
 */

public class JsonUtil {

    /**
     * 封装Json格式的返回成功时的状态，返回成功提示信息，并根据需要返回json对象或json数组(不需要返回时传null)
     * @param message 输入成功信息
     * @param object 根据需要返回json对象
     * @param arr 根据需要返回json数组
     * @return json格式的请求成功信息,state代表状态(成功或失败)，meta含成功提示信息,data含json格式的
     * 返回数据对象(一条数据)或json数组(一组同类数据)
     * @throws IllegalArgumentException
     */

    public static String successJson(String message, JSONObject object, JSONArray arr)
            throws IllegalArgumentException{
        JSONObject json=new JSONObject();
        JSONObject object1=new JSONObject();
        object1.put("state","success");
        object1.put("message",message);
        json.put("meta", object1);
        if(object!=null)
            json.put("data", object);
        if(arr!=null)
            json.put("data", arr);
        return json.toString();
    }

    /**
     * 封装Json格式的返回失败时的状态，返回失败提示信息
     * @param message 返回失败信息
     * @return json格式的请求失败信息,state代表状态(成功或失败)，meta含失败提示信息
     */
    public static String failedJson(String message){
        JSONObject json=new JSONObject();
        JSONObject object1=new JSONObject();
        object1.put("state","failed" );
        object1.put("message",message);
        json.put("meta", object1);
        return json.toString();
    }

    public static JSONObject changeToJsonObj(Object obj){
        if(obj!=null){
            return  new JSONObject().fromObject(obj);
        }else{
            return  new JSONObject();
        }
    }

    /**
     * 将JSON数组转换成excel文件
     * @param jsonArray
     */
    public static SXSSFWorkbook JSONToExcel(JSONArray jsonArray,String type){
            int k=0;
            if(type.equals("warn"))
                k=1;
            else if(type.equals("normal"))
                k=2;
            String[][] heads={{"id","createTime","ipAddress","machineName","moduleName","operatorType","type","username","errorRunningInfo"},
                    {"id","warningType","warningLevel","warningModuleName","type","createTime","username","ipAddress","warningInfo"},
                    {"id","type","createTime","username","result","operateInfo"}};
            SXSSFWorkbook workbook = new SXSSFWorkbook();// 创建一个Excel文件
            SXSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet
            SXSSFRow titleRow = sheet.createRow(0);//创建表头
            for(int i=0;i<heads[k].length;i++){//表头赋值
                titleRow.createCell(i).setCellValue(heads[k][i]);
            }
            if (jsonArray.size() > 0) {//单元格赋值
                for (int i = 0; i < jsonArray.size(); i++) {
                    SXSSFRow row = sheet.createRow(i + 1);
                    JSONObject json = jsonArray.getJSONObject(i); // 遍历 jsonarray
                    for(int j=0;j<heads[k].length;j++){
                        row.createCell(j).setCellValue(json.get(heads[k][j]).toString());//赋值
                    }

                }
            }
            return workbook;
        }

}
